package com.onegene.pdf.component.report.cancer;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.layout.Style;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.*;
import com.itextpdf.layout.layout.RootLayoutArea;
import com.itextpdf.layout.property.AreaBreakType;
import com.itextpdf.layout.property.HorizontalAlignment;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;
import com.onegene.pdf.component.AbstractReportBuilder;
import com.onegene.pdf.component.IReportBuilder;
import com.onegene.pdf.component.entity.PrintReportBean;
import com.onegene.pdf.component.entity.ReportBean;
import com.onegene.pdf.component.event.CancerHeaderTextEvent;
import com.onegene.pdf.component.event.PageNumberEvent;
import com.onegene.pdf.component.factory.ReportBuilderContext;
import com.onegene.pdf.component.paint.CancerPainting;
import com.onegene.pdf.component.report.CommonHandler;
import com.onegene.pdf.component.report.gene.GenoColor;
import com.onegene.pdf.component.report.gene.GenoComponent;
import com.onegene.pdf.component.report.gene.GenoStyle;
import com.onegene.pdf.util.Precondition;
import org.springframework.util.StringUtils;

import java.net.MalformedURLException;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: laoliangliang
 * @description: 易感简化版开发（标准版通用模板）
 * @create: 2020/7/3 15:21
 **/
public class CancerTenReportBuilder extends AbstractReportBuilder {

    private java.util.List<ReportBean.ItemsBean> gaoLevel;
    private java.util.List<ReportBean.ItemsBean> normalLecel;
    private java.util.Set<ReportBean.CategoriesBean> normalLecelCategory;

    private java.util.List<String> excludeList = Lists.newArrayList("mrhf", "jfss", "tfqn", "gxtz", "wss", "kwz");


    public CancerTenReportBuilder(ReportBuilderContext builderContext) {
        super(builderContext);
        // 设置字体
        builderContext.setFontType("cancer");
    }

    @Override
    public IReportBuilder addIndex() {
        ReportBean.IndexBean index = reportBean.getIndex();
        doc.setMargins(50, 55, 20, 55);
        doc.setCharacterSpacing(0.6f);
        doc.setFontSize(9f);
        doc.setFontColor(ColorConstants.BLACK);

        Div headerDiv = new Div();
        headerDiv.setPaddingTop(-10);
        headerDiv.setWidth(UnitValue.createPercentValue(100));
        Paragraph header = new Paragraph();
        header.setWidth(UnitValue.createPercentValue(100));
        String gender = "MALE".equals(index.getGender()) ? "男士" : "FEMALE".equals(index.getGender()) ? "女士" : "";
        header.add(new Text(gender + index.getPackageName().replaceAll("\\(.*\\)", "").replaceAll("（.*）", "") + "报告单").setFontSize(18).setBold().setFontColor(CancerColor.getThemeColor()));
        PageSize pageSize = pdf.getDefaultPageSize();

        // 如果没有额外logo，则壹基因logo放右边
        String logoImageUrl = builderContext.getLogoImageUrl();
        Image logoImage = new Image(ImageDataFactory.create(CancerTenReportBuilder.class.getClassLoader().getResource("image/cancer/logo.png")));
        if (logoImageUrl != null) {
            logoImage.setFixedPosition(pageSize.getWidth() - 168, pageSize.getHeight() - 73);
            logoImage.setHeight(25).setWidth(50);
        } else {
            logoImage.setFixedPosition(pageSize.getWidth() - 106, pageSize.getHeight() - 73);
            logoImage.setHeight(25).setWidth(50);
        }
        header.add(logoImage);

        // 如果有额外logo，放右边
        if (logoImageUrl != null) {
            try {
                Image indexImage = new Image(ImageDataFactory.create(logoImageUrl));
                indexImage.setFixedPosition(pageSize.getWidth() - 102, pageSize.getHeight() - 75);
                indexImage.setHeight(30).setWidth(45);
                header.add(indexImage);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        headerDiv.add(header);
        doc.add(headerDiv);

        CancerPainting cancerPainting = new CancerPainting(pdf, font);
        if (logoImageUrl != null) {
            cancerPainting.drawHeaderLine(true);
        } else {
            cancerPainting.drawHeaderLine(false);
        }
        cancerPainting.close();
        return this;
    }

    @Override
    public IReportBuilder addHello() {
        return null;
    }

    @Override
    public IReportBuilder addExaminee() {
        ReportBean.IndexBean index = reportBean.getIndex();

        Div div = new Div();
        Paragraph p1 = new Paragraph();
        p1.add(new Text("受检者信息").addStyle(CancerStyle.getSecondTitleStyle()));
        div.add(p1);
        doc.add(div);

        Div div2 = new Div();
        div2.addStyle(GenoStyle.getDefaultTableOuter().setBorder(new SolidBorder(GenoColor.getThemeColor(), 1f)));
        Table table = new Table(3).useAllAvailableWidth();
        table.addCell(CancerComponent.getDefaultCell().add(CancerComponent.getExamineeParagraph("姓 名：" + index.getName())));
        table.addCell(CancerComponent.getDefaultCell().add(CancerComponent.getExamineeParagraph("性 别： " + ("FEMALE".equals(index.getGender()) ? "女" : "MALE".equals(index.getGender()) ? "男" : ""))));
        table.addCell(CancerComponent.getDefaultCell().add(CancerComponent.getExamineeParagraph("年 龄：" + (index.getBirthYear() == null ? "" : (DateUtil.year(new Date()) - index.getBirthYear())))));
        table.startNewRow();
        table.addCell(CancerComponent.getDefaultCell().add(CancerComponent.getExamineeParagraph("样本编号：" + index.getSampleCode())));
        table.addCell(CancerComponent.getDefaultCell().add(CancerComponent.getExamineeParagraph("样本类型：口腔粘膜细胞")));
        table.startNewRow();
        table.addCell(CancerComponent.getDefaultCell().add(CancerComponent.getExamineeParagraph("样本检测结果：合格")));
        table.addCell(CancerComponent.getDefaultCell().add(CancerComponent.getExamineeParagraph("检测方法：高通量基因分型技术")));
        div2.add(table);
        doc.add(div2);
        return this;
    }

    @Override
    public IReportBuilder addDetectionContent() {
        Paragraph p3 = new Paragraph();
        p3.add(new Text("检测结果").addStyle(CancerStyle.getSecondTitleStyle()));
        doc.add(p3);

        gaoLevel = new LinkedList<>();
        normalLecel = new LinkedList<>();
        normalLecelCategory = new LinkedHashSet<>();
        java.util.List<ReportBean.CategoriesBean> categories = reportBean.getCategories();
        for (ReportBean.CategoriesBean category : categories) {
            java.util.List<ReportBean.CategoriesBean.ItemsBean> items = category.getItems();
            for (ReportBean.CategoriesBean.ItemsBean item : items) {
                if (!item.getLocked()) {
                    ReportBean.ItemsBean itemsBean = reportBean.getItems().get(item.getCode());
                    itemsBean.setCategoryRank(category.getRank());
                    if (Integer.parseInt(item.getLevel()) >= 3) {
                        gaoLevel.add(itemsBean);
                    } else if (Integer.parseInt(item.getLevel()) < 3) {
                        normalLecel.add(itemsBean);
                        normalLecelCategory.add(category);
                    }
                }
            }
        }
//        CommonHandler.sortGaoLevel(gaoLevel);
        gaoLevel.addAll(normalLecel);

        Table geneLocusTable = new Table(3).useAllAvailableWidth();
        geneLocusTable.addCell(CancerComponent.getTableCell().setBorderRight(new SolidBorder(ColorConstants.WHITE, 1)).add(new Paragraph("检测项目")).addStyle(CancerStyle.getTableHeaderStyle()));
        geneLocusTable.addCell(CancerComponent.getTableCell().setBorderRight(new SolidBorder(ColorConstants.WHITE, 1)).add(new Paragraph("风险指数")).addStyle(CancerStyle.getTableHeaderStyle()));
        geneLocusTable.addCell(CancerComponent.getTableCell().add(new Paragraph("风险等级")).addStyle(CancerStyle.getTableHeaderStyle()));
        for (ReportBean.ItemsBean itemsBean : gaoLevel) {
            geneLocusTable.startNewRow();
            geneLocusTable.addCell(CancerComponent.getTableCell().add(new Paragraph(itemsBean.getName()).addStyle(CancerStyle.getTableContextStyle())));
            geneLocusTable.addCell(CancerComponent.getTableCell().add(new Paragraph(itemsBean.getScore() + "").addStyle(CancerStyle.getTableContextStyle())));
            geneLocusTable.addCell(CancerComponent.getTableCell().add(new Paragraph(itemsBean.getLabel()).addStyle(CancerStyle.getTableContextStyle()).addStyle(CancerColor.getLabelColor(itemsBean))));
        }
        doc.add(geneLocusTable);
        return this;
    }

    @Override
    public IReportBuilder addResultSummary() {
        // 用户须知
        Div userNoticeDiv = new Div();
        userNoticeDiv.setKeepTogether(true);
        // 用户须知
        userNoticeDiv.add(new Paragraph("用户须知").addStyle(CancerStyle.getSecondTitleStyle()).setMarginTop(13));
        userNoticeDiv.add(new Paragraph(new Text(
                "1.疾病遗传风险等级用基因风险指数来解释和划分，分级及其标准为：低风险<0.5，0.5≤ 稍低风险<0.8，0.8≤正常风险<1.2，1.2≤稍高风险<1.8，高风险≥1.8。 \n" +
                        "2.壹基因选取的检测基因和位点均基于国际权威数据库(HGMD、OMIM等)和权威学术研究报道。 \n" +
                        "3.疾病的发生与基因变异密切相关，是基因(内因)与生活方式(外因)共同作用的结果。基因检测可用于指导完善生活方式、疾病预防干预措施和风险规避方案，不作为临床诊断和治疗的依据。 \n" +
                        "4.特别提示：基因检测的目的不是去忽视低风险疾病，而是提前排查出高风险疾病，并重点对其进行针对性地预防和干预，以延缓或规避遗传高风险疾病的发生。 \n" +
                        "5.如果您对我们检测服务和体验有任何意见或建议，敬请拨打我们的健康热线400-163-5588")).setFontSize(9).setMarginTop(-4).setMarginBottom(10).setTextAlignment(TextAlignment.JUSTIFIED));

        // 签名印章
        Table signatureTable = new Table(2);
        signatureTable.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        signatureTable.setWidth(UnitValue.createPercentValue(50));
        signatureTable.addCell(GenoComponent.getSignCell().add(CancerComponent.getSignParagraph("检测人：舒平")));
        signatureTable.addCell(GenoComponent.getSignCell().add(CancerComponent.getSignParagraph("复核人：张萍")));
        signatureTable.startNewRow();
        signatureTable.addCell(GenoComponent.getSignCell().add(CancerComponent.getSignParagraph("日期：" + (DateUtil.format(new Date(), "yyyy年M月d日")))));
        signatureTable.addCell(GenoComponent.getSignCell().add(CancerComponent.getSignParagraph("日期：" + (DateUtil.format(new Date(), "yyyy年M月d日")))));
        userNoticeDiv.add(signatureTable);
        doc.add(userNoticeDiv);
        RootLayoutArea layoutArea = (RootLayoutArea) doc.getRenderer().getCurrentArea();
        Rectangle bBox = layoutArea.getBBox();

        // 画印章
        CancerPainting cancerPainting = new CancerPainting(pdf, font);
        cancerPainting.drawStamper(bBox, builderContext);
        cancerPainting.close();
        return this;
    }

    @Override
    public IReportBuilder addContext() {
        doc.setMargins(50, 55, 50, 55);
        PageNumberEvent pageEvent = new PageNumberEvent(doc, -1);
        pdf.addEventHandler(PdfDocumentEvent.START_PAGE, pageEvent);
        CancerHeaderTextEvent headerTextEvent = new CancerHeaderTextEvent("重点注意项", font);
        pdf.addEventHandler(PdfDocumentEvent.START_PAGE, headerTextEvent);

        gaoLevel.removeAll(normalLecel);
        for (ReportBean.ItemsBean itemsBean : gaoLevel) {
            doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));

            // 项目风险描述 + 基因位点信息
            addDescibleSummary(itemsBean, "\n在相同外界条件下，您患" + itemsBean.getName() + "的" + itemsBean.getLabel() + "于正常⼈群。建议您尽可能规避" + itemsBean.getName() + "相关⾼危因素、调整⽣活习惯，改变不利的⽣活⽅式，定期进⾏体检和相关筛查，认真对待相关急慢性疾病(如发⽣)的治疗和医疗⼲预。", 2);

            // 预防体检建议
            addHealthSuggest(itemsBean, 2);

        }

        // 中简化版，劣势项不能和正常项连在一起
        if (builderContext.getVersion() == 1) {
            doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
        }

        // 移除监听器
        pdf.removeEventHandler(PdfDocumentEvent.START_PAGE, headerTextEvent);

        // 添加正常项目
        for (ReportBean.CategoriesBean categoriesBean : normalLecelCategory) {
            int unlockedItems = categoriesBean.getUnlockedItems();
            if (unlockedItems == 0) {
                continue;
            }

            CancerHeaderTextEvent bodyTextEvent = new CancerHeaderTextEvent(categoriesBean.getName(), font);
            pdf.addEventHandler(PdfDocumentEvent.START_PAGE, bodyTextEvent);

            java.util.List<ReportBean.CategoriesBean.ItemsBean> items = categoriesBean.getItems();
            int pageNumber1 = 0;
            int pageNumber2 = 0;
            for (ReportBean.CategoriesBean.ItemsBean item : items) {
                ReportBean.ItemsBean itemsBean = reportBean.getItems().get(item.getCode());
                if (item.getLocked() || item.getIndex() >= 3) {
                    continue;
                }

                // 大简化版也需要换页
                if ((pageNumber2 > pageNumber1 || builderContext.getVersion() != 1)) {
                    doc.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
                }

                // 项目风险描述+基因位点信息
                addDescibleSummary(itemsBean, "在相同外界条件下，您与⼤多数⼈的患病" +
                        "⻛险相同。基因⻛险相同情况下，最终患病与否，与外界环境、⽣活习惯和医疗条件的差" +
                        "异密切相关。因此，仍建议您重视预防" + itemsBean.getName() + "，注意规避外界⻛险因素，保持良好的⽣" +
                        "活习惯，加强锻炼，定期进⾏体检和相关筛查。", 1);

                pageNumber1 = pdf.getNumberOfPages();

                // 预防体检建议
                addHealthSuggest(itemsBean, 1);

                pageNumber2 = pdf.getNumberOfPages();


            }
            pdf.removeEventHandler(PdfDocumentEvent.START_PAGE, bodyTextEvent);
        }
        pdf.removeEventHandler(PdfDocumentEvent.START_PAGE, pageEvent);
        return this;
    }

    /**
     * 预防体检建议
     * level 1-正常项目 2-劣势项目
     */
    private void addHealthSuggest(ReportBean.ItemsBean itemsBean, Integer level) {
        Div blockDiv = new Div();
        List<ReportBean.ItemsBean.ContentsBean> contents = itemsBean.getContents();

        float offsetBottom = -1;
        // 简介，症状，健康建议，基因解读
        for (ReportBean.ItemsBean.ContentsBean content : contents) {
            if (!("健康建议-线下".equals(content.getLabel()) || "提升建议-线下".equals(content.getLabel()) ||
                    "减肥建议-线下".equals(content.getLabel()) || "护理建议-线下".equals(content.getLabel()) ||
                    "体检建议".equals(content.getLabel()) || "预防建议".equals(content.getLabel())
            )) {
                continue;
            }
            if (!builderContext.getAll()) {
                // 大简化版，不显示模块2（预防建议）
                if (builderContext.getVersion() == 2 && "预防建议".equals(content.getLabel())) {
                    continue;
                }

                // 大简化版劣势详细版，劣势项目显示模块2(等于正常项目不显示模块2)
                if (builderContext.getVersion() == 4 && "预防建议".equals(content.getLabel()) && level == 1) {
                    continue;
                }

                // 中简化版，正常项目两模块均不显示（体检建议和预防建议）
                if (builderContext.getVersion() == 1 && level == 1) {
                    continue;
                }
            }
            // 非疾病类仍取线下字段，对应不同的建议
            if (content.getLabel().endsWith("线下") && excludeList.contains(itemsBean.getCategoryCode())) {
                if (("潜能特质类".equals(itemsBean.getCategoryName()))) {
                    Paragraph p3 = new Paragraph("提升建议").addStyle(CancerStyle.getThirdTitleStyle().setMarginBottom(offsetBottom));
                    blockDiv.add(p3);
                } else if (("减肥瘦身类".equals(itemsBean.getCategoryName()))) {
                    Paragraph p3 = new Paragraph("减肥建议").addStyle(CancerStyle.getThirdTitleStyle().setMarginBottom(offsetBottom));
                    blockDiv.add(p3);
                } else if (("美容护肤类".equals(itemsBean.getCategoryName()))) {
                    Paragraph p3 = new Paragraph("护肤建议").addStyle(CancerStyle.getThirdTitleStyle().setMarginBottom(offsetBottom));
                    blockDiv.add(p3);
                } else if (("个性特质类".equals(itemsBean.getCategoryName()) || "维生素需求类".equals(itemsBean.getCategoryName()) ||
                        "矿物质需求类".equals(itemsBean.getCategoryName()))) {
                    Paragraph p3 = new Paragraph("健康建议").addStyle(CancerStyle.getThirdTitleStyle().setMarginBottom(offsetBottom));
                    blockDiv.add(p3);
                }
            } else {
                // 疾病类不需要取线下，专取体检建议和预防建议
                if (!content.getLabel().endsWith("线下")) {
                    Paragraph p3 = new Paragraph(content.getLabel()).addStyle(CancerStyle.getThirdTitleStyle().setMarginBottom(offsetBottom));
                    blockDiv.add(p3);
                } else {
                    continue;
                }
            }
            Div overall = new Div();
            String value = content.getValue();
            if (StringUtils.isEmpty(value)) {
                continue;
            }
            List<IElement> iElements = getFixContent(value);
            handlerHtmlParagraph(overall, iElements);
//            defaultHandlerHtml(overall, iElements);
            blockDiv.add(overall);
        }
        doc.add(blockDiv);
    }

    /**
     * 直接通过itext7转换html文本成元素组件添加到文本域
     * 优点：可将大多html样式直接转化
     * 缺点：体积较大，随数据量增大可能比一般浏览器下载下来的大3倍以上（数据小的时候不明显）
     */
    private void defaultHandlerHtml(Div overall, List<IElement> iElements) {
        for (IElement iElement : iElements) {
            Style style = new Style();
            style.setFontSize(9);
            style.setTextAlignment(TextAlignment.JUSTIFIED);
            style.setCharacterSpacing(0.7f);
            if (iElement instanceof Div) {
                Div div = (Div) iElement;
                List<IElement> children = div.getChildren();
                // 全部段落改成相同样式
                CommonHandler.addParagraphStyleCircle(style, children);
                overall.add(div);
            } else if (iElement instanceof Paragraph) {
                Paragraph element = (Paragraph) iElement;
                overall.add(element.addStyle(style));
            }
        }
    }

    /**
     * 通过itext7转换html文本成元素组件后取出文本，自行组装成段落（Paragraph）
     * 优点：此方法优势在于可以大量减小PDF体积，数据越多可大约减少三分之二的大小
     * 缺点：目前只支持字体颜色及粗体两种html样式，后续若需要添加需要修改代码
     */
    private void handlerHtmlParagraph(Div overall, java.util.List<IElement> iElements) {
        Style style = new Style();
        style.setFontSize(9);
        style.setTextAlignment(TextAlignment.JUSTIFIED);
        style.setCharacterSpacing(0.7f);
        style.setMarginTop(0);
        style.setMarginBottom(0);
        Paragraph paragraph = new Paragraph().addStyle(style);
        paragraph.setFixedLeading(18f);
        for (IElement iElement : iElements) {
            CommonHandler.handlerElement(iElement, paragraph);
        }
        overall.add(paragraph);
    }

    /**
     * 项目风险描述 + 基因位点信息
     *
     * @param itemsBean
     * @param level     1-正常项目 2-劣势项目
     */
    private void addDescibleSummary(ReportBean.ItemsBean itemsBean, String context, int level) {
        Div blockDiv = new Div();
        blockDiv.setMarginTop(10);
        blockDiv.setKeepTogether(true);
        Paragraph p1 = new Paragraph();
        String name = itemsBean.getName();
        p1.add(name);
        p1.addStyle(CancerStyle.getItemTitleStyle());
        p1.setMinWidth(30);
        if (name.length() > 6) {
            p1.setWidth(UnitValue.createPercentValue(20 + (name.length() - 6) * 3));
        } else {
            p1.setWidth(UnitValue.createPercentValue(20));
        }
        p1.setTextAlignment(TextAlignment.CENTER);
        p1.setMarginBottom(0);
        blockDiv.add(p1);


        Table table = new Table(2).useAllAvailableWidth();
        table.setKeepTogether(true);
        table.setBorder(new SolidBorder(CancerColor.getThemeColor(), 1));
        table.addCell(CancerComponent.getSimpleDescCell().setWidth(UnitValue.createPercentValue(13)).add(new Paragraph(new Text(itemsBean.getLabel()).addStyle(CancerStyle.getOff())).addStyle(CancerStyle.getItemLabelStyle(itemsBean)).setPaddingTop(0)));
        Paragraph p2 = new Paragraph(new Text("您的基因风险指数为" + itemsBean.getScore() + "，" + itemsBean.getName() + itemsBean.getLabel() + "。").addStyle(CancerColor.getLabelColor(itemsBean)));
        p2.setFixedLeading(12);
        p2.add(new Text(context).setFontColor(ColorConstants.BLACK));
        table.addCell(GenoComponent.getDefaultCell(2, 1).add(p2));
        table.startNewRow();
        table.addCell(CancerComponent.getSimpleDescCell().setPaddingTop(-8).setWidth(UnitValue.createPercentValue(13)).add(new Paragraph(new Text("风险指数 " + itemsBean.getScore()).addStyle(CancerStyle.getOff2())).addStyle(CancerStyle.getItemLabelStyle(itemsBean).setPaddingTop(0).setFontSize(9f))));
        blockDiv.add(table);

        blockDiv.add(new Paragraph("基因位点信息").addStyle(CancerStyle.getThirdTitleStyle()));
        List<ReportBean.ItemsBean.GeneDescBean> geneDesc = itemsBean.getGeneDesc();
        Table geneLocusTable = new Table(4).useAllAvailableWidth();
        if (builderContext.getVersion() == 1 && level == 1) {
            geneLocusTable.setMarginBottom(20);
        }
        geneLocusTable.addCell(CancerComponent.getTableCell().setBorderRight(new SolidBorder(ColorConstants.WHITE, 1)).addStyle(CancerStyle.getGeneDescCellStyle()).add(new Paragraph("基因位点")).addStyle(CancerStyle.getTableHeader()));
        geneLocusTable.addCell(CancerComponent.getTableCell().setBorderRight(new SolidBorder(ColorConstants.WHITE, 1)).addStyle(CancerStyle.getGeneDescCellStyle()).add(new Paragraph("参考型")).addStyle(CancerStyle.getTableHeader()));
        geneLocusTable.addCell(CancerComponent.getTableCell().setBorderRight(new SolidBorder(ColorConstants.WHITE, 1)).addStyle(CancerStyle.getGeneDescCellStyle()).add(new Paragraph("检出型")).addStyle(CancerStyle.getTableHeader()));
        geneLocusTable.addCell(CancerComponent.getTableCell().addStyle(CancerStyle.getGeneDescCellStyle()).add(new Paragraph("基因型解释")).addStyle(CancerStyle.getTableHeader()));
        if (geneDesc.size() > 16) {
            geneDesc = geneDesc.subList(0, 16);
        }
        for (ReportBean.ItemsBean.GeneDescBean geneDescBean : geneDesc) {
            geneLocusTable.startNewRow();
            geneLocusTable.addCell(CancerComponent.getTableCell().addStyle(CancerStyle.getGeneDescCellStyle()).add(new Paragraph(geneDescBean.getOg_id()).setFontColor(ColorConstants.BLACK)));
            geneLocusTable.addCell(CancerComponent.getTableCell().addStyle(CancerStyle.getGeneDescCellStyle()).add(new Paragraph(geneDescBean.getRef_genotype()).setFontColor(ColorConstants.BLACK)));
            geneLocusTable.addCell(CancerComponent.getTableCell().addStyle(CancerStyle.getGeneDescCellStyle()).add(new Paragraph(geneDescBean.getGenotype()).setFontColor(ColorConstants.BLACK)));
            geneLocusTable.addCell(CancerComponent.getTableCell().addStyle(CancerStyle.getGeneDescCellStyle()).add(new Paragraph(geneDescBean.getLabel()).setFontColor(ColorConstants.BLACK)));
        }
        blockDiv.add(geneLocusTable);
        doc.add(blockDiv);
    }

    @Override
    public IReportBuilder addThanks() {
        return null;
    }

    @Override
    public IReportBuilder addBackCover() {
        return null;
    }

    @Override
    public IReportBuilder addCatalog() {
        return null;
    }

    @Override
    public IReportBuilder addPageNumber() {
        return null;
    }

    @Override
    public IReportBuilder buildAll(PrintReportBean data) {
        if (builderContext.getPart() == 0) {
            this.reportBean = data;
            Precondition.checkNotNull(reportBean, "报告数据为空");
            addIndex();
            addHello();
            addExaminee();
            addDetectionContent();
            addResultSummary();
            // 简化版只显示到用户须知
            if (builderContext.getVersion() != 3) {
                addContext();
                addThanks();
                addBackCover();
                addCatalog();
                addPageNumber();
            }
            build();
        } else {
            invokePartProxy(data);
        }
        return this;
    }

    private java.util.List<IElement> getFixContent(String content) {
        content = content.replaceAll("<p>", "<div>");
        content = content.replaceAll("</p>", "</div>");
        if (content.startsWith("<div>")) {
            content = content.replaceAll("<div>", "<div style='line-height:18pt;font-size:14px;'>");
        } else {
            content = "<div style='line-height:18pt;font-size:14px;'>" + content + "</div>";
        }
        content = content.replaceAll("<br>", "</br>");
        return HtmlConverter.convertToElements(content, proper);
    }


}
